package cc.mrbird.febs.system.service.impl;


import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.ListUtil;
import cc.mrbird.febs.system.entity.DrawlingMark;
import cc.mrbird.febs.system.mapper.DrawlingMarkMapper;
import cc.mrbird.febs.system.service.IDrawlingMarkService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 *  Service实现
 *
 * @author MrBird
 * @date 2019-07-20 23:16:03
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DrawlingMarkServiceImpl extends ServiceImpl<DrawlingMarkMapper, DrawlingMark> implements IDrawlingMarkService {

    @Resource
    private DrawlingMarkMapper drawlingMarkMapper;

    @Override
    public IPage<DrawlingMark> findDrawlingMarks(QueryRequest request, DrawlingMark drawlingMark) {
        LambdaQueryWrapper<DrawlingMark> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<DrawlingMark> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<DrawlingMark> findDrawlingMarks(DrawlingMark drawlingMark) {
	    LambdaQueryWrapper<DrawlingMark> queryWrapper = new LambdaQueryWrapper<>();
	    if(null!=drawlingMark){

	        if(null!=drawlingMark.getDrawingId()){
                queryWrapper.eq(DrawlingMark::getDrawingId,drawlingMark.getDrawingId());
            }
            if(null!=drawlingMark.getMarkId()){
                queryWrapper.eq(DrawlingMark::getMarkId,drawlingMark.getMarkId());
            }

        }

	    // TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createDrawlingMark(DrawlingMark drawlingMark) {
        Integer drawingId=drawlingMark.getDrawingId();
        String markId= drawlingMark.getMarkIds();
        //this.save(drawlingMark);
        if(StringUtils.isNotBlank(markId)){
            // 获取本次操作对象
            String[] markIds = markId.split(StringPool.COMMA);
            List<String> markIdList= Arrays.asList(markIds);
            //获取已已存在的对象
            List<String>  list=   drawlingMarkMapper.findByMarkId(drawlingMark.getDrawingId());
            if(CollectionUtils.isNotEmpty(list)){
                //获取需要删除的对象
                List<String> deleteList= ListUtil.getDifference(markIdList,list);
                deleteList.forEach(id->{
                    DrawlingMark data=new DrawlingMark();
                    data.setDrawingId(drawingId);
                    data.setMarkId(Integer.valueOf(id));
                    this.deleteDrawlingMark(data);
                });
                //获取需要添加的对象
                List<String> addList= ListUtil.getDifference(list,markIdList);
                addList.forEach(id->{
                    DrawlingMark data=new DrawlingMark();
                    data.setDrawingId(drawingId);
                    data.setMarkId(Integer.valueOf(id));
                    this.save(data);
                });

            }else{
                //获取需要添加的对象
                markIdList.forEach(id->{
                    DrawlingMark data=new DrawlingMark();
                    data.setDrawingId(drawingId);
                    data.setMarkId(Integer.valueOf(id));
                    this.save(data);
                });
            }
        }else{//删除所有备注
            DrawlingMark data=new DrawlingMark();
            data.setDrawingId(drawingId);
            this.deleteDrawlingMark(data);
        }
    }

    @Override
    @Transactional
    public void updateDrawlingMark(DrawlingMark drawlingMark) {
        this.saveOrUpdate(drawlingMark);
    }

    @Override
    @Transactional
    public void deleteDrawlingMark(DrawlingMark drawlingMark) {
        LambdaQueryWrapper<DrawlingMark> wapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
        if(null!=drawlingMark){
            if(null!=drawlingMark.getMarkId()){
                wapper.eq(DrawlingMark::getMarkId,drawlingMark.getMarkId());
            }
            if(null!=drawlingMark.getDrawingId()){
                wapper.eq(DrawlingMark::getDrawingId,drawlingMark.getDrawingId());
            }

        }

	    this.remove(wapper);
	}
}
